﻿using System;
using System.Collections.Generic;
using System.IO;
using EmbeddedSharp.Scripting;
using Zove.Runtime.Queries;
using Zove.Runtime.Core;
using Zove.Runtime.Core.Zoom;

namespace Zove.Scripting.Core {

    /// <summary>
    /// The command line mode.
    /// </summary>
    public class ZoveScriptingConsole : ScriptingConsole {

        /// <summary>
        /// Displayed title
        /// </summary>
        public override string Title
        {
            get
            {
                return "ZoveScript Console";
            }
        }

        /// <summary>
        /// Welcoming message
        /// </summary>
        public override string WelcomeMessage
        {
            get
            {
                return "ZoveScript Console\nCopyright 2009 Team Zove";
            }
        }

        /// <summary>
        /// Main routine
        /// </summary>
        /// <param name="args"></param>
        public static new void Main(string[] args) {
            ZoveScriptingConsole console = new ZoveScriptingConsole();

            console.Process(args);
        }

        /// <summary>
        /// Run the file
        /// </summary>
        /// <param name="fileName"></param>
        protected override void Process(string fileName)
        {
            string source = File.ReadAllText(fileName);

            if (fileName.EndsWith(".zql"))
            {
                QueryManager qm = new QueryManager(new ZoomLanguageNature());
                Query query = qm.CreateQuery(source);
                Dictionary<string, QueryResult> results = query.Run();

                Console.WriteLine("Result:");
                foreach (QueryResult r in results.Values)
                {
                    Console.WriteLine(r);
                }
            } else if (fileName.EndsWith(".zapp")) {
                ZoveScriptingObject script = new ZoveScriptingObject(source);
                script.Run();
            } else {
                Console.WriteLine("Unkown file type");
            }
        }

        protected override ScriptingObject GetVirtualMachine()
        {
            return new ZoveScriptingObject();
        }
    }
}
